package com.zy.controller;

import com.zy.bean.bo.version.VersionCreateBo;
import com.zy.bean.bo.version.VersionUpdateBo;
import com.zy.bean.vo.VersionVo;
import com.zy.config.SpringContextHolder;
import com.zy.service.EdgeService;
import com.zy.service.LiteflowChainService;
import com.zy.service.NodeService;
import com.zy.service.VersionListService;
import com.zy.util.VersionUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.fatewa.engine.advice.protocol.RayquazaResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.atomic.AtomicReference;

import static org.fatewa.engine.genius.utils.GenericUtils.transaction;

/**
 * 版本相关的接口
 *
 * @author hanhuafeng
 * @version V1.0
 * @description
 * @date 2023/7/19
 */
@RestController
@RequestMapping("/v1/version")
@RequiredArgsConstructor
public class VersionController {
    private final VersionUtil versionUtil;
    private final VersionListService versionListService;
    private final EdgeService edgeService;
    private final NodeService nodeService;
    private final LiteflowChainService liteflowChainService;

    /**
     * 创建版本,如果存在历史版本，则复制历史版本的流程图内容
     *
     * @param versionCreateBo 版本创建bo
     */
    @PostMapping
    public RayquazaResponse createVersion(@Validated @RequestBody VersionCreateBo versionCreateBo) {
        // 获取对应chartCode的最近一次版本号
        String latestVersionNum = versionListService.getLatestVersionNum(versionCreateBo.getChartCode());
        AtomicReference<String> newVersionCode = new AtomicReference<>("");
        transaction((manager, status) -> {
            newVersionCode.set(versionUtil.createVersion(versionCreateBo.getVersionName(),
                    versionCreateBo.getIntroduce(),
                    versionCreateBo.getChartCode(),
                    versionCreateBo.getInitParam()));
            if (ObjectUtils.isNotEmpty(latestVersionNum)) {
                // 说明存在历史版本，迭代历史版本的流程图内容
                edgeService.copyEdge(versionCreateBo.getChartCode(), latestVersionNum, newVersionCode.get());
                nodeService.copyNode(versionCreateBo.getChartCode(), latestVersionNum, newVersionCode.get());
                liteflowChainService.copyFlowChain(versionCreateBo.getChartCode(), latestVersionNum, newVersionCode.get());
            }
        }, SpringContextHolder.getApplicationContext());
        return new RayquazaResponse(newVersionCode);
    }

    /**
     * 更新版本
     *
     * @param versionCode     版本code
     * @param versionUpdateBo 版本更新bo
     */
    @PatchMapping("/{versionCode}")
    public void updateVersion(@PathVariable String versionCode,
                              @Validated @RequestBody VersionUpdateBo versionUpdateBo) {
        versionListService.updateVersion(versionCode, versionUpdateBo);
    }

    /**
     * 根据版本code获取版本信息
     *
     * @param versionCode 版本code
     * @return VersionVo 版本信息
     */
    @GetMapping("/{versionCode}")
    public VersionVo getVersion(@PathVariable String versionCode) {
        return versionListService.getVersionByCode(versionCode);
    }
}
